package sde.actor.railway;
import sde.actor.*;
import java.io.*;

public class SensorePeriodico extends Sensore{ //Solo dimostrativo
	private static class NextSample extends Message{}
	private ControlloreIF controller; //porta di uscita - acquaintance
	private NextSample next=new NextSample();
	private double periodo;
	private BufferedReader br;
	private File sensorStream;
	public SensorePeriodico( ControlloreIF controller, double periodo, File sensorStream ){
		this.controller=controller;
		this.periodo=periodo;
		this.sensorStream=sensorStream;
		try{
		     br=new BufferedReader( new FileReader(sensorStream) );
		}catch(IOException e){ e.printStackTrace(); System.exit(-1); }
		send( next, now()+periodo );
	}
	public void handler( Message m ){
		if( m==next ){
		      sample();
		      send( next, now()+periodo );
		}
	}//handler
	public void sample(){//comportamento di test
		String s=null;
		try{
			if( (s=br.readLine())!=null ){
			     if( s.equalsIgnoreCase("a") )
			            controller.send( new ControlloreIF.Arrival() );
			    else if( s.equalsIgnoreCase("p") )
			            controller.send( new ControlloreIF.Departure() );
			    }
			else{
			     br.close();
			     br=new BufferedReader( new FileReader(sensorStream) );
			}
		}catch(IOException e){}
	 }//sample
}//SensorePeriodico

